import {ADD_TODO_ITEM, CHANGE_INPUT_VALUE, DEL_TODO_ITEM, INIT_LIST_ACTION} from './actionType';

const defaultState = {
    inputValue: '',
    list: []
};

export default (state = defaultState, action) => {
    if (action.type === CHANGE_INPUT_VALUE) {
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue = action.value;
        return newState;
    }
    if (action.type === ADD_TODO_ITEM) {
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.unshift(newState.inputValue);
        newState.inputValue = '';
        return newState
    }
    if (action.type === DEL_TODO_ITEM) {
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.splice(action.index, 1);
        return newState
    }
    if (action.type === INIT_LIST_ACTION) {
        return {inputValue: '', list: action.data}
    }
    return state
}